//
//  QCouponCodeVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/3/27.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QCouponCodeVC.h"

@interface QCouponCodeVC ()
{
    UITextField * _tf;
}

@end

@implementation QCouponCodeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"兑换券码";
    [self setupForDismissKeyboard];
    [self createUI];
}
- (void)requestData{
    WS(wSelf);
    if (_tf.text.length == 0) {
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请输入券码" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        return;
    }
    [self.view endEditing:YES];
    NSString *url = [QParamService urlWithAction:@"UserAccessCard"];
    NSDictionary *param = [QParamService UserAccessCardWithType:@"1" Card_code:_tf.text];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationCouponSuccess object:nil];
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:[basModel.items objectForKey:@"msg"] btnTitle:showAlertBtnTitle type:1 myBlock:^{
                [wSelf.navigationController popViewControllerAnimated:YES];
            }];

        }else{
            
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
    

}
- (void)createUI{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 76, kScreenWidth, 60)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    UIView * lineView1 = [UIView new];
    [view addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo (@0);
        make.top.equalTo (@0);
        make.height.equalTo (@1);
        make.right.equalTo (@0);
    }];
    lineView1.backgroundColor = [QColorUtility defaultLineColor];
    
    UIView * lineView2 = [UIView new];
    [view addSubview:lineView2];
    lineView2.backgroundColor = [QColorUtility defaultLineColor];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo (@0);
        make.height.equalTo (@1);
        make.left.equalTo (@0);
        make.right.equalTo (@0);
    }];
    
    _tf = [[UITextField alloc]initWithFrame:CGRectMake(20, 5, kScreenWidth - 40, 50)];
    [view addSubview:_tf];
    _tf.font = [UIFont systemFontOfSize:16];
    _tf.placeholder = @"请输入兑换券码";
    _tf.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    UIButton * convertBtn = [QViewUtility createButtonWithFrame:CGRectMake(20, 176, kScreenWidth - 40, 45) font:16 title:@"兑 换" titleColor:[UIColor whiteColor] backgroundColor:[QColorUtility defaultBlueColor]];
    [self.view addSubview:convertBtn];
    convertBtn.layer.cornerRadius = 7;
    convertBtn.layer.masksToBounds = YES;
    [convertBtn addTarget:self action:@selector(requestData) forControlEvents:UIControlEventTouchUpInside];
    [convertBtn blueStyle];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
